﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WorkSpace.UI.Utils
{
    public class HScrollingWrapper : FrameworkElement
    {
        public ScrollViewer Target
        {
            get
            {
                return (ScrollViewer)GetValue(TargetProperty);
            }
            set
            {
                SetValue(TargetProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for Target.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TargetProperty =
            DependencyProperty.Register("Target", typeof(ScrollViewer), typeof(HScrollingWrapper), new PropertyMetadata(null, OnTargetChanged));


        static void OnTargetChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            HScrollingWrapper sender = (HScrollingWrapper)source;
            sender.UpdateMargin();
        }

        public double Value
        {
            get
            {
                return (double)GetValue(ValueProperty);
            }
            set
            {
                SetValue(ValueProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(double), typeof(HScrollingWrapper), new PropertyMetadata(0.0, OnValueChanged));

        static void OnValueChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            HScrollingWrapper sender = (HScrollingWrapper)source;
            sender.UpdateMargin();
        }

        private void UpdateMargin()
        {
            if (Target != null)
            {
                Target.ScrollToHorizontalOffset(Value);
                //var thicknessProperty = Target.GetType().GetProperty(PropertyName);
                //var currentThickness = (Thickness)thicknessProperty.GetValue(Target, null);
                //var nextThickness = new Thickness(
                //    CalculateThickness(Side.Left, currentThickness.Left),
                //    CalculateThickness(Side.Top, currentThickness.Top),
                //    CalculateThickness(Side.Right, currentThickness.Right),
                //    CalculateThickness(Side.Bottom, currentThickness.Bottom)
                //    );

                //thicknessProperty.SetValue(Target, nextThickness, null);
            }
        }
    }
}
